package com.yanqu.road.dao.impl.cross.banquet;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.banquet.baifubanquet.GoodBanquetData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author cwq
 * @Data 2021/11/15 14:12
 */
public class CrossGoodBanquetDaoImpl  extends TempDataObjectDao<GoodBanquetData> {

    public static String getGoodBanquetDataSql = "select * from `t_c_activity_good_banquet` where `activityId`=?  order by `id` asc ";
    public List<GoodBanquetData> getGoodBanquetData(int activityId) {
        ResultSet rs = null;
        CallableStatement call = null;
        List<GoodBanquetData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getGoodBanquetDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                list.add(parse(rs));
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return list;
    }

    public static String updateGoodBanquetDataSql = "update `t_c_activity_good_banquet` set userId=?,type=?,num=?,isSettlement=?,endTime=?,createTime=? where id=? and activityId=?";
    @Override
    protected boolean updateData(GoodBanquetData goodBanquetData) {
        goodBanquetData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, goodBanquetData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, goodBanquetData.getNum()));
        para.put(i++, new DbParameter(Types.BOOLEAN, goodBanquetData.getIsSettlement()));
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetData.getEndTime()));
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetData.getCreateTime()));
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetData.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, goodBanquetData.getActivityId()));
        if (getDb().executeNoneQuery(updateGoodBanquetDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String insertGoodBanquetDataSql = "insert into `t_c_activity_good_banquet`(id,activityId,userId,type,num,isSettlement,endTime,createTime) values (?,?,?,?,?,?,?,?)";
    @Override
    protected boolean addData(GoodBanquetData goodBanquetData) {
        goodBanquetData.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetData.getId()));
        para.put(i++, new DbParameter(Types.INTEGER, goodBanquetData.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetData.getUserId()));
        para.put(i++, new DbParameter(Types.INTEGER, goodBanquetData.getType()));
        para.put(i++, new DbParameter(Types.INTEGER, goodBanquetData.getNum()));
        para.put(i++, new DbParameter(Types.BOOLEAN, goodBanquetData.getIsSettlement()));
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetData.getEndTime()));
        para.put(i++, new DbParameter(Types.BIGINT, goodBanquetData.getCreateTime()));
        if (getDb().executeNoneQuery(insertGoodBanquetDataSql, para) > 0) {
            return true;
        }
        return false;

    }

    @Override
    protected GoodBanquetData parseData(ResultSet rs) throws SQLException {
        GoodBanquetData goodBanquetData = new GoodBanquetData();
        goodBanquetData.setId(rs.getLong("id"));
        goodBanquetData.setActivityId(rs.getInt("activityId"));
        goodBanquetData.setUserId(rs.getLong("userId"));
        goodBanquetData.setType(rs.getInt("type"));
        goodBanquetData.setNum(rs.getInt("num"));
        goodBanquetData.setIsSettlement(rs.getBoolean("isSettlement"));
        goodBanquetData.setEndTime(rs.getLong("endTime"));
        goodBanquetData.setCreateTime(rs.getLong("createTime"));

        return goodBanquetData;
    }


    public Long getMaxRecordId() {
        String sql = "select ifnull(max(id),0) as maxId from t_c_activity_good_banquet";
        ResultSet rs = null;
        CallableStatement call = null;
        long maxId = 0L;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getLong("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }

}
